package dream.web;

import dream.pojo.AppVar;
import dream.pojo.User;
import dream.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    private UserService service = new UserService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取用户信息
        String username = request.getParameter("username").trim();
        String password = request.getParameter("password").trim();
        String code = request.getParameter("code");

        //判断输入框是否为空
        if(username.equals("")||password.equals("")||code.equals("")){
            // 存储错误信息到request
            request.setAttribute("login_msg", "输入框不能为空");

            //跳转到login.jsp
            request.getRequestDispatcher("/login_new.jsp").forward(request, response);
            return;
        }

        //判断验证码是否正确
        //程序生成的验证码，从Session中获取
        HttpSession session = request.getSession();
        String checkCodeGen = (String) session.getAttribute("checkCodeGen");

        //对比
        if(!checkCodeGen.equalsIgnoreCase(code)){
            request.setAttribute("login_msg", "验证码错误");
            request.getRequestDispatcher("/login_new.jsp").forward(request, response);
            return;
        }

        //调用service查询
        User user = service.login(username, password);
        if(user != null){
            //登陆成功
            String ids = null;
            int id = user.getId();
            try {
                MessageDigest digest = MessageDigest.getInstance("md5");
                digest.update(("nothing" + user.getId()).getBytes());
                ids = new BigInteger(1, digest.digest()).toString(16);
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }

            //储存用户id和账号到浏览器
            Cookie cookie = new Cookie("ids", ids);
            cookie.setMaxAge(60 * 60 * 24 * 2);
            cookie.setHttpOnly(true);
            response.addCookie(cookie);

            Cookie cookie1 = new Cookie("id", String.valueOf(user.getId()));
            cookie1.setMaxAge(60 * 60 * 24 * 2);
            cookie1.setHttpOnly(true);
            response.addCookie(cookie1);

            //将当前登录的用户保存到session中
            //得到session对象
            HttpSession session1 = request.getSession(true);
            if(session1 != null){
                session1.setAttribute(AppVar.SESSION_USERINFO_KEY, user);
            }

            request.getRequestDispatcher("/person_rewrite.html").forward(request, response);

        }else {
            //登陆失败
            request.setAttribute("login_msg", "账号或密码错误");

            //跳转到login.jsp
            request.getRequestDispatcher("/login_new.jsp").forward(request, response);
        }

    }
}
